﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using UtilZ.Dotnet.WindowsDesktopEx.WPF;

namespace WpfApp.Test
{
    /// <summary>
    /// TestEmbedAppProcess.xaml 的交互逻辑
    /// </summary>
    public partial class TestEmbedAppProcess : Window
    {
        public TestEmbedAppProcess()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 移除窗口边框消息索引
        /// </summary>
        private const int _GWL_STYLE = (-16);

        /// <summary>
        /// 
        /// </summary>
        private const int _WS_CHILD = 0x40000000;

        /// <summary>
        /// 移除窗口边框消息编号
        /// </summary>
        private const int _WS_VISIBLE = 0x10000000;
        private const string DLL_USER32 = "user32.dll";

        [DllImport(DLL_USER32, SetLastError = true)]
        internal static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport(DLL_USER32, EntryPoint = "MoveWindow", SetLastError = true)]
        internal static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

        [DllImport(DLL_USER32, EntryPoint = "SetWindowLongA", SetLastError = true)]
        internal static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private WpfAppEmbedder _colorEmbedApp = null;
        private void btnColor_Click(object sender, RoutedEventArgs e)
        {
            //var win = Window.GetWindow(borderColor);
            //Point point = borderColor.TransformToAncestor(win).Transform(new Point(0, 0));
            //MessageBox.Show($"x:{point.X},Y:{point.Y}");

            if (this._colorEmbedApp != null)
            {
                return;
            }

            this._colorEmbedApp = new WpfAppEmbedder(borderColor, @"E:\Soft\Color\Color.exe", false);
            this._colorEmbedApp.Start();

            //-------------------------------------------------------------------------

            // var process = new Process();
            // process.StartInfo.FileName = @"E:\Soft\Color\Color.exe";
            // process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            // process.Start();
            // while (process.MainWindowHandle == IntPtr.Zero)
            // {
            //     Thread.Sleep(10);
            // }

            // var appWin = process.MainWindowHandle;
            //// var hwnd = new WindowInteropHelper(this).Handle;
            // //var hwndSource = System.Windows.PresentationSource.FromVisual(grid) as System.Windows.Interop.HwndSource;
            // var hwndSource = System.Windows.PresentationSource.FromVisual(borderColor) as System.Windows.Interop.HwndSource;
            // var hwnd = hwndSource.Handle;

            // SetWindowLong(appWin, _GWL_STYLE, _WS_VISIBLE);
            // SetParent(appWin, hwnd);
            // MoveWindow(appWin, 0, 0, (int)borderColor.ActualWidth, (int)borderColor.ActualHeight, true);
        }

        private WpfAppEmbedder _netAssistEmbedApp = null;
        private void btnNotepad_Click(object sender, RoutedEventArgs e)
        {
            if (this._netAssistEmbedApp != null)
            {
                return;
            }

            //this._netAssistEmbedApp = new WpfEmbedApp(panel, @"E:\Soft\NetAssist.exe");
            this._netAssistEmbedApp = new WpfAppEmbedder(borderNotepad, @"C:\Program Files (x86)\Notepad++\notepad++.exe", false);
            this._netAssistEmbedApp.Start();
        }

        private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabControl.SelectedIndex == 0)
            {
                if (this._colorEmbedApp != null)
                {
                    this._colorEmbedApp.Visible = true;
                }

                if (this._netAssistEmbedApp != null)
                {
                    this._netAssistEmbedApp.Visible = false;
                }
            }
            else
            {
                if (this._colorEmbedApp != null)
                {
                    this._colorEmbedApp.Visible = false;
                }

                if (this._netAssistEmbedApp != null)
                {
                    this._netAssistEmbedApp.Visible = true;
                }
            }
        }
    }
}
